package com.leyou.user.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.auth.mapper.BrandMapper;
import com.leyou.search.pojo.Brand;
import com.leyou.search.pojo.BrandDTO;
import com.leyou.service.IBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {

        PageHelper.startPage(page, rows);

        Example example = new Example(Brand.class);

        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%").orEqualTo("letter", key.toUpperCase());
        }

        if (StringUtils.isNotBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }

        List<Brand> brands = brandMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }

        PageInfo<Brand> info = new PageInfo<>(brands);

        List<BrandDTO> list = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        return new PageResult<>(info.getTotal(), list);
    }

    @Override
    @Transactional
    public void saveBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);

        brand.setId(null);

        int count = brandMapper.insertSelective(brand);

        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }

        count = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        int count = brandMapper.updateByPrimaryKeySelective(brand);

        if (count != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }

        int query = brandMapper.queryCountByBid(brand.getId());
        count = brandMapper.deleteCategoryBrand(brand.getId());
        if (count != query) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }

        count = brandMapper.insertCategoryBrand(brand.getId(), cids);
        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public Boolean deleteBrand(Long bid) {
        try {
            int count = brandMapper.deleteByPrimaryKey(bid);
            if (count != 1) {
                throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
            }

            int query = brandMapper.queryCountByBid(bid);
            count = brandMapper.deleteCategoryBrand(bid);
            if (count != query) {
                throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
            }

            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public BrandDTO queryById(Long brandId) {
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByCid(Long cid) {
        List<Brand> list = brandMapper.queryBrandByCid(cid);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,BrandDTO.class);
    }

    @Override
    public List<BrandDTO> queryBrandByIds(List<Long> bids) {
        List<Brand> brands = brandMapper.selectByIdList(bids);
        if (CollectionUtils.isEmpty(brands)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands,BrandDTO.class);

    }


}
